import Taro from '@tarojs/taro'

const environment = 'http://localhost:7001';

// 添加拦截器
const interceptor = function (chain) {
    // 请求信息
    const requestParams = chain.requestParams
    // const { method, data, url } = requestParams
    // console.log(`http ${method || 'GET'} --> ${url} data: `, data)
    // 使用chain.proceed调用下一个拦截器或发起请求
    // const token = Taro.getStorageSync('token');
    // if (token) {
    //     requestParams.header.token = token
    // }
    return chain.proceed(requestParams)
        .then(res => {
            // 响应信息
            // console.log(`http <-- ${url} result:`, res)
            return res
        })

}

Taro.addInterceptor(interceptor)

export default {
    request(url, data, method) {
        return new Promise((resolve, reject) => {
            Taro.showLoading();
            Taro.request({
                url,
                data,
                method,
            }).then(res => {
                resolve(res);
                Taro.hideLoading();
            }).catch(err => {
                reject(err);
                Taro.hideLoading();
            })
        })
    },
    get(url, data) {
        return this.request(url, data, 'GET');
    },
    post(url, data) {
        return this.request(url, data, 'POST');
    },
    environment
}
